package com.salt.modules.sys.service;

import com.salt.common.service.CrudService;
import com.salt.modules.sys.entity.Dock;
import com.salt.modules.sys.entity.Menu;
import com.salt.modules.sys.mapper.DockMapper;
import com.salt.modules.sys.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author salt
 * @Date 2021/12/23
 * @Version 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class DockService extends CrudService<DockMapper, Dock> {

    @Transactional(readOnly = false)
    public void save(Dock dock) {
        dock.preInsert();
        mapper.insert(dock);
    }

    public List<Menu> findByDock(Dock dock) {
        List<Dock> list = mapper.findList(dock);
        if (list != null && !list.isEmpty()) {
            List<Menu> menuAll = UserUtils.getMenuList(new Menu(dock.getTenant(), null, "2"));
            List<Menu> menuList = new ArrayList<>(list.size());
            for (Dock dockVal:list) {
                for (Menu menu:menuAll) {
                   if (dockVal.getMenuId().equals(menu.getId())) {
                       menu.setSort(dockVal.getSort());
                       menu.setDockId(dockVal.getId());
                       menuList.add(menu);
                   }
                }
            }
            return menuList;
        }
        return null;
    }
}
